﻿using Contrib.VimeoField.Settings;
using System.Collections.Generic;
using System.Text;

namespace Contrib.VimeoField.ViewModels
{
    public class VimeoFieldViewModel
    {
            // sets vimeo defaults
            // based on http://developer.vimeo.com/player/embedding
        /*
            ShowTitle = true;
            ShowByline = true;
            ControlsColor = "00adef";
            ShowPortrait = true;
            Autoplay = false;
            Loop = false;
            JavascriptApiEnable = true;
            PlayerId = "";            
        */
        public const string VimeoBaseUrl = "https://player.vimeo.com/video/{0}?";
        public int Width { get; set; }
        public int Height { get; set; }
        public string Url { get; set; }
        public string VideoId { get; set; }
        private bool ShowUserPortrait { get; set; }
        private bool ShowByline { get; set; }
        private bool Autoplay { get; set; }
        private bool ShowTitle { get; set; }
        private string ControlsColor { get; set; }
        public string PlayerId { get; set; }
        private bool JavascriptApiEnable { get; set; }
        private bool Loop { get; set; }
        public string Name { get; set; }    

        public VimeoFieldViewModel(Fields.VimeoField vimeoField )
        {
            var settings = vimeoField.PartFieldDefinition.Settings.GetModel<VimeoFieldSettings>();
            Name = vimeoField.Name;
            VideoId = vimeoField.VideoId;
            Width = vimeoField.Width != 0 ? vimeoField.Width: settings.DefaultWidth;
            Height = vimeoField.Height != 0 ? vimeoField.Height : settings.DefaultHeight;
            ShowUserPortrait = settings.ShowUserPortrait;
            ShowTitle = settings.ShowTitle;
            Autoplay = settings.Autoplay;
            ShowByline = settings.ShowTitle;
            Autoplay = settings.Autoplay;
            Loop = settings.Loop;
            JavascriptApiEnable = settings.JavascriptApiEnable;
            PlayerId = settings.PlayerId;
            ControlsColor = settings.ControlsColor;
            StringBuilder url = new StringBuilder();
            url.AppendFormat(VimeoBaseUrl, VideoId);

            Dictionary<string,string> urlParams = new Dictionary<string, string>();
            if (ControlsColor.Length > 0)
                urlParams.Add("color", ControlsColor.Replace("#", ""));
            urlParams.Add("title", ShowTitle ? "1" : "0");
            urlParams.Add("byline", ShowByline ? "1" : "0");
            urlParams.Add("portrait", ShowUserPortrait ? "1" : "0");
            urlParams.Add("autoplay", Autoplay ? "1" : "0");
            urlParams.Add("loop", Loop ? "1" : "0");
            urlParams.Add("api",JavascriptApiEnable?"1":"0");
            if (PlayerId.Length > 0)
                urlParams.Add("player_id", PlayerId);
            foreach (KeyValuePair<string,string> p in urlParams)
            {
                url.AppendFormat("{0}={1}&", p.Key, System.Web.HttpUtility.UrlEncode(p.Value));
            }
            Url = url.ToString();

        }

        public void UpdateField(Fields.VimeoField vimeoField)
        {
            vimeoField.VideoId = VideoId;           
            vimeoField.Width = Width;
            vimeoField.Height = Height;            
        }

    }
}